package com.qyy.remotemonitor.common.util;

import android.Manifest;
import android.app.Application;
import android.content.Context;
import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;

import com.blankj.utilcode.util.LogUtils;

import javax.annotation.Nonnull;

/**
 * Created by qinyy on 2018/10/21.
 */

public class LocationManagerUtil
{
    private String TAG = getClass().getSimpleName();
    private static LocationManagerUtil sLocationManagerUtil;
    private Context mContext;
    private LocationManager mLocationManager;
    private String mCurrentProvider;

    public double getLat()
    {
        if (mLocationManager != null)
        {
            if (ActivityCompat
                    .checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED && ActivityCompat
                    .checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED)
            {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
            }
            else
                return mLocationManager.getLastKnownLocation(mCurrentProvider).getLatitude();
        }
        return 0;
    }


    public double getLon()
    {
        if (mLocationManager != null)
        {

            if (ActivityCompat
                    .checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED && ActivityCompat
                    .checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) !=
                    PackageManager.PERMISSION_GRANTED)
            {
                // TODO: Consider calling
                //    ActivityCompat#requestPermissions
                // here to request the missing permissions, and then overriding
                //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
                //                                          int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for ActivityCompat#requestPermissions for more details.
            }
            else
                return mLocationManager.getLastKnownLocation(mCurrentProvider).getLongitude();
        }
        return 0;
    }


    public static LocationManagerUtil getInstance(@Nonnull Application context)
    {
        if (sLocationManagerUtil == null)
            sLocationManagerUtil = new LocationManagerUtil(context);
        return sLocationManagerUtil;
    }


    private LocationManagerUtil(@Nonnull Application context)
    {
        mContext = context;
        mLocationManager = (LocationManager) context
                .getSystemService(Context.LOCATION_SERVICE);

    }


    /**
     * 初始化位置信息
     */
    public void initLocation()
    {
        LogUtils.dTag(TAG,"initLocation");
        if(ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(mContext, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            LogUtils.dTag(TAG,"had no location permission");
            return;
        }
        mCurrentProvider = LocationManager.NETWORK_PROVIDER;
        // 是否开启GPS定位
        if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER))
        {
            mCurrentProvider = LocationManager.GPS_PROVIDER;
        }

        LogUtils.dTag(TAG,"initLocation current provide is " + mCurrentProvider);

        mLocationManager.requestLocationUpdates(mCurrentProvider, 30 * 1000, 20, mLocationListener);

    }



    LocationListener mLocationListener = new LocationListener()
    {
        @Override
        public void onLocationChanged(Location location)
        {
            LogUtils.dTag(TAG,"onLocationChanged  " + location.toString());
        }

        @Override
        public void onStatusChanged(String s, int i, Bundle bundle)
        {
            LogUtils.dTag(TAG,"onStatusChanged  " + s );
        }

        @Override
        public void onProviderEnabled(String s)
        {
            LogUtils.dTag(TAG,"onProviderEnabled  " + s );
            mLocationManager.removeUpdates(mLocationListener);
            initLocation();
        }

        @Override
        public void onProviderDisabled(String s)
        {
            LogUtils.dTag(TAG,"onProviderDisabled  " + s );
            mLocationManager.removeUpdates(mLocationListener);
            initLocation();
        }
    };

}
